#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define ep emplace
#define eb emplace_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++) 
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;

int read() {
	int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-', ch = getchar(); 
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return f ? -x : x;
}

const int N = 510;
const int M = N * N;
const int inf = 0x3f3f3f3f;

int n, m, T;
veg G[M], tG[M];
vec pot;
int col[N << 3], w[N << 3], bel[M], tot, K, ptot;
vec bpot[M];
int tdis[M], dis[110][110], f[110][110];

inline int id(int x, int y) { return x * (m + 1) + y; }
inline int lid(int x) {
	if(x <= m) return id(0, x);
	else if(x <= m + n) return id(x - m, m);
	else if(x <= 2 * m + n) return id(n, m - (x - m - n));
	else return id(n - (x - m * 2 - n), 0);
}

void dijk(int id, int *dis) {
	priority_queue < pii, vector < pii >, greater < pii > > q;
	memset(tdis, 0x3f, sizeof tdis); dis[id] = 0;
	for(auto v : bpot[id]) tdis[v] = 0, q.ep(0, v); int cnt = 0;
	while(q.size() && cnt < tot) {
		int x = q.top().se, d = q.top().fi; q.pop(); if(tdis[x] < d) continue;
		//	cerr << id << " " << x << " " << bel[x] << " " << tdis[x] << " " << endl;
		if(bel[x] && dis[bel[x]] > tdis[x]) dis[bel[x]] = tdis[x], cnt++;
		for(auto v : G[x]) if(tdis[v.fi] > tdis[x] + v.se) { tdis[v.fi] = tdis[x] + v.se; q.ep(tdis[v.fi], v.fi); }
	}
}

void solve() {
	K = in; tot = 0;
	memset(col, -1, sizeof col);
	memset(w, 0, sizeof w);
	memset(bel, 0, sizeof bel);
	memset(dis, 0x3f, sizeof dis);
	memset(f, 0, sizeof f);
	rep(i, 0, (n + 1) * (m + 1)) G[i] = tG[i];
	int totcol = 0;
	while(K--) {
		int x = in, p = in, t = in;
		w[p] = x; col[p] = t; totcol |= 1 << col[p];
	}
	if(totcol ^ 3) return puts("0"), void();
	rep(i, 1, m) {
		int x = id(0, i - 1), y = id(0, i);
		G[x].eb(y, w[i]); G[y].eb(x, w[i]);
	}
	rep(i, 1, n) {
		int x = id(i - 1, m), y = id(i, m);
		G[x].eb(y, w[i + m]); G[y].eb(x, w[i + m]);
	}
	per(i, m, 1) {
		int x = id(n, i - 1), y = id(n, i);
		G[x].eb(y, w[m - i + 1 + m + n]); G[y].eb(x, w[m - i + 1 + m + n]);
	}
	per(i, n, 1) {
		int x = id(i - 1, 0), y = id(i, 0);
		G[x].eb(y, w[n - i + 1 + m + m + n]); G[y].eb(x, w[n - i + 1 + m + m + n]);
	}
	rep(i, 1, n + n + m + m) {
		if(col[i] == -1) continue;
		rep(j, i + 1, n + n + m + m + 1) {
			if(j == n + n + m + m + 1) j = 1;
			if(col[j] == -1) continue;
			if(col[j] ^ col[i]) {
				tot++; bpot[tot].clear();
				int x = lid(i), y = lid(j);
				bool fl = false;
				for(auto v : pot) {
					if(v == x) fl = true;
					if(v == y && fl) break;
					else if(fl) bel[v] = tot, bpot[tot].eb(v);
				}
			}
			break;
		}
	}
	assert(tot % 2 == 0);
	rep(i, 1, tot) dijk(i, dis[i]);
	//rep(i, 1, tot) { cerr << "!" << endl; for(auto v : bpot[i]) cerr << v << " " ; cerr <<endl; }
	//cerr << tot << endl;
	//rep(i, 1, tot) { rep(j, 1, tot) cerr << dis[i][j] << " "; cerr << endl; }
	rep(len, 2, tot) {
		rep(l, 1, tot - len + 1) {
			int r = l + len - 1;
			f[l][r] = inf;
			rep(k, l + 1, r) { f[l][r] = min(f[l][r], f[l + 1][k - 1] + dis[l][k] + f[k + 1][r]); k++; }
		}
		len++;
	} printf("%d\n", f[1][tot]);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin); freopen("traffic.out", "w", stdout);
#endif
	n = in, m = in, T = in; ptot = n * m;
	rep(_, 1, 2) {
		rep(i, 0, m) pot.eb(id(0, i));
		rep(i, 1, n - 1) pot.eb(id(i, m));
		per(i, m, 0) pot.eb(id(n, i));
		per(i, n - 1, 1) pot.eb(id(i, 0));
	}
	rep(i, 1, n - 1)
		rep(j, 1, m) {
		int w = in, x = id(i, j - 1), y = id(i, j);
		G[x].eb(y, w); G[y].eb(x, w);
	}
	rep(i, 1, n)
		rep(j, 1, m - 1) {
		int w = in, x = id(i - 1, j), y = id(i, j);
		G[x].eb(y, w); G[y].eb(x, w);
	}
	rep(i, 0, (n + 1) * (m + 1)) tG[i] = G[i];
	while(T--) solve();
	return 0;
}
